package name.zxw.rabbitmq.exchange;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmCallback;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author zhangxinwang
 * @date 2022/6/22 16:44
 */
public class MyPublisher {

    public static void main(String[] args) throws Exception {
        String exchangeName = "logs";

        ConnectionFactory factory = new ConnectionFactory();
        Channel channel = factory.newConnection().createChannel();

        ConfirmCallback ackCallback = (deliveryTag, multiple) -> {
            System.out.println("确认回调:");
            System.out.println("deliveryTag = " + deliveryTag);
            System.out.println("multiple = " + multiple);
        };
        ConfirmCallback nackCallback = (deliveryTag, multiple) -> {
            System.out.println("确认失败回调:");
            System.out.println("deliveryTag = " + deliveryTag);
            System.out.println("multiple = " + multiple);
        };

        channel.addConfirmListener(ackCallback, nackCallback);

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT);

        Scanner scanner = new Scanner(System.in);
        System.out.println("等待发送消息...");
        while(scanner.hasNext()){
            String message = scanner.next();
            channel.basicPublish(exchangeName,"aaa",null,message.getBytes(StandardCharsets.UTF_8));
            System.out.println("发送信息:"+message);
        }


    }


}
